package com.digitalart.nav {
	
	//import com.digitalart.section.photogallery.GalleryController;
	//import com.digitalart.section.photogallery.GalleryModel;
	import com.digitalart.commons.CustomAssetLoader;
	import com.digitalart.commons.CustomEvent;
	import com.digitalart.nav.item.NavItem;
	import com.digitalart.nav.item.NavItemController;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	
	
	/**
	 * @author ulises.ocana
	 */
	public class NavController extends EventDispatcher {
		
		
		public static const CHANGE_SECTION_REQUEST : String = "changeSectionRequest";
		public static const ON_LOAD_COMPLETE : String = "onLoadComplete";
		
		private var _navModel : NavModel;
		private var _navView : NavView;
		private var _assetLoader : CustomAssetLoader;
		private var _navItemControllerList : Array;
		
		
		/**********************************Constructor*************************/
		
		public function NavController() {
			
			super(this);
			_navItemControllerList = new Array();
			_navModel = new NavModel();
			_navModel.addEventListener(NavModel.ON_ITEMS_CREATED, createView);
			_assetLoader = CustomAssetLoader.getInstance();                                                                 
		}
		
		
		/**********************************************************************/
		
		/**********************************Methods*************************/
		
		private function createView(e:Event):void {
			
			_navView = new NavView();
			_assetLoader.addEventListener(CustomAssetLoader.ON_ASSET_LOAD_COMPLETE, completeLoadBackGround);
			_assetLoader.loadAsset(_navModel.backGroundPath);                        
		}
		
		private function completeLoadBackGround(e : CustomEvent) : void {
			
			e.target.removeEventListener(CustomAssetLoader.ON_ASSET_LOAD_COMPLETE, completeLoadBackGround);
			_assetLoader.removeEventListener(Event.COMPLETE, completeLoadBackGround);
			_navView.backgroundImage = e.data as Bitmap;
			_assetLoader.addEventListener(CustomAssetLoader.ON_ASSET_LOAD_COMPLETE, completeLoadLogo);
			_assetLoader.loadAsset(_navModel.logoPath);
		}
		
		private function completeLoadLogo(e : CustomEvent) : void {
			
			e.target.removeEventListener(CustomAssetLoader.ON_ASSET_LOAD_COMPLETE, completeLoadBackGround);
			_navView.logoImage = e.data as Bitmap;
			_navView.addImages();
			_navView.addLogoContainerText(_navModel.logoContainerText);
			_navView.homeLogoContainer.addEventListener(MouseEvent.CLICK, onHomeClicked);
			addNavItems();                                                             
		}
		
		private function onHomeClicked(event : MouseEvent) : void {     
			
			var itemView : NavItem;
			for each(var controller : NavItemController in _navItemControllerList){                         
				itemView = controller.getView();
				itemView.isSelected = false;
			}
			checkSelected();                
			dispatchEvent(new CustomEvent(CHANGE_SECTION_REQUEST, MainController.MAIN_SECTION_ALIAS));
		}
		
		private function addNavItems() : void {
			
			var itemController : NavItemController;                 
			for each(var navItemVO : NavItemVO in _navModel.navItems) {
				itemController = new NavItemController(navItemVO);
				itemController.addEventListener(NavItemController.ITEM_CLICKED, onChangeSectionRequest);
				_navItemControllerList.push(itemController);
			}
			locateNavItems();                                        
		}
		
		private function locateNavItems():void{
			
			var navItemsContainer : Sprite = new Sprite();
			var itemView : NavItem;
			var itemSpacer : ItemSpacer;
			var lastX : Number = 0;
			
			for(var i : int = 0; i < _navItemControllerList.length; i++){                           
				itemView = (_navItemControllerList[i] as NavItemController).getView();
				itemView.x = lastX;
				
				itemSpacer = new ItemSpacer();                                                                  
				navItemsContainer.addChild(itemView);
				itemSpacer.x = itemView.x + itemView.width + 1;
				itemSpacer.y = 3;
				if((_navItemControllerList.length - 1) != i){
					navItemsContainer.addChild(itemSpacer); 
				}                                       
				lastX = itemSpacer.x + itemSpacer.width + 2;                                                                                                                    
			}
			navItemsContainer.x = _navView.width - navItemsContainer.width - 24;
			navItemsContainer.y = 14;
			_navView.addChild(navItemsContainer);
			dispatchEvent(new Event(ON_LOAD_COMPLETE));     
		}
		
		private function onChangeSectionRequest(e : CustomEvent) : void {
			
			checkSelected();                        
			dispatchEvent(new CustomEvent(CHANGE_SECTION_REQUEST,e.data));                  
		}
		
		private function checkSelected() : void {
			
			var itemView : NavItem;
			for each(var controller : NavItemController in _navItemControllerList){                         
				itemView = controller.getView();
				if(itemView.isSelected){
					itemView.setTextColorSelected();
				}else{
					itemView.setTextColorUnSelected();
				}
				itemView.isSelected = false;
			}
		}
		
		/**********************************************************************/
		/**************************Getters & Setters***************************/
		
		public function getView() : Sprite {
			return _navView;
		}
	}
	}